package com.good.msgsys.controller;

import com.good.msgsys.dto.Result;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.Paper;
import com.good.msgsys.service.PaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 10:18
 */
@Api(tags = "论文模块")
@RestController
@RequestMapping("/api/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;

    // 新增论文
    @ApiOperation("新增论文")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result<Paper> add(@ApiParam("论文实体") @RequestBody Paper paper) {
        paperService.add(paper);
        return new Result<>(ResultCode.SUCCESS, paper);
    }

    // 根据id删除论文
    @ApiOperation("根据id删除论文")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result<Paper> deleteById(@ApiParam("论文id") @PathVariable("id") String id){
        paperService.deleteById(id);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 更新论文
    @ApiOperation("更新论文")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result<Paper> update(@ApiParam("论文id") @PathVariable("id") String id, @ApiParam("论文实体") @RequestBody Paper paper){
        paper.setId(id);
        paperService.update(paper);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 根据id查询论文
    @ApiOperation("根据id查询论文")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Paper> findById(@ApiParam("论文id") @PathVariable("id") String id) {
        Paper paper = paperService.findById(id);
        return new Result<>(ResultCode.SUCCESS, paper);
    }

    // 查询所有论文
    @ApiOperation("查询所有论文")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result<List<Paper>> findAll() {
        List<Paper> papers = paperService.findAll();
        return new Result<>(ResultCode.SUCCESS, papers);
    }
}
